package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.PageHelperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by 甘静斌 on 2020-08-07 13:41
 */
@Service
//@Transactional
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyCommentService strategyCommentService;
   

    @Override
    public void save(StrategyComment strategyComment) {
        //如果id为 "" ,spring-data-mongodb 不会自动生成id注入
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {

        repository.save(strategyComment);
    }

    @Override
    public StrategyComment get(String id) {
        return repository.findById(id).orElse(null);
    }


    @Override
    public List<StrategyComment> list() {


        return repository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {

        //查询封装类
        Query query = new Query();
        //分页数据 , currentPage , pageSite
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize() ,Sort.Direction.ASC,"_id");
        //后台游记管理,查询出author作者的数据
        Page<StrategyComment> page = PageHelperUtil.query(template, pageable, StrategyComment.class, query, qo);
        //list Pageable total 封装三个数据,返回给页面
        return page;
    }

    @Override
    public void commentThumb(String cid, UserInfo userInfo) {

        //通过cid获取当前评论的对象
        StrategyComment comment = strategyCommentService.get(cid);
        //通过对象获取到评论点赞的集合
        List<String> idList = comment.getThumbuplist();
        //获取userInfo的id
        String uid = userInfo.getId();
        //判断uid是否在集合中
        if (idList.contains(uid)){
            //如果存在,点赞数 - 1
            int thumbupnum = comment.getThumbupnum();
            comment.setThumbupnum(thumbupnum -1);
            idList.remove(uid);
        }else {
            //如果不存在 ,点赞数 + 1
            int thumbupnum = comment.getThumbupnum();
            comment.setThumbupnum(thumbupnum + 1);
            idList.add(uid);
        }
        //更新
        strategyCommentService.update(comment);
    }



}
